﻿package Interface.MessagesClasses
{	
	import Core.Game;
	
	import Interface.TracksClasses.TrackButton;
	
	import social.VKontakte.VKEvents;
	import social.SocialDataEvent;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.MouseEvent;		

	public class TransferMessage extends Sprite
	{	
		private var FTrasferButton:TrackButton;
		private var FCancelButton:TrackButton;
		private var FVoices:TextField;
		private var FMoney:TextField;
		private var FGame:Game;
		
		public function TransferMessage(AGame:Game):void 
		{
			FGame = AGame;
			FVoices = getChildByName('VoicesText') as TextField; 
			FVoices.restrict = "0-9";
			FMoney = getChildByName('MoneyText') as TextField; 
			FTrasferButton = getChildByName('Transfer') as TrackButton;			
			FCancelButton = getChildByName('Cancel') as TrackButton;
			FTrasferButton.Text = 'Перевод';
			FCancelButton.Text = 'Отмена';
			FMoney.addEventListener(Event.ENTER_FRAME, OnVoicesChange);
			FTrasferButton.addEventListener(MouseEvent.MOUSE_DOWN, OnTrasferButton);
		}
		
		public function get TransferButton():TrackButton
		{
			return FTrasferButton;
		}
		
		public function get CancelButton():TrackButton
		{
			return FCancelButton;
		}
		
		public function get Voices():String
		{
			return FVoices.text;
		}
		
		public function get Money():String
		{
			return FMoney.text;
		}
		
		private function OnVoicesChange(e:Event):void
		{
			FMoney.text = (int(FVoices.text) * 1000).toString();
		}
		
		private function OnTrasferButton(e:MouseEvent):void
		{
			FGame.Provider.DirectTransfer(int(FVoices.text));
			trace(int(FVoices.text));
			FGame.Provider.addEventListener(VKEvents.VK_VOICES_NEEDED, OnVoicesNeeded);
			FGame.Provider.addEventListener(VKEvents.VK_VOICES_WITHDRAWED, OnVoicesWithdrawed);
		}
		
		private function OnVoicesNeeded(e:SocialDataEvent):void
		{
			FGame.Provider.removeEventListener(VKEvents.VK_VOICES_NEEDED, OnVoicesNeeded);
			FGame.ShowInfo('На Вашем счету недостаточно голосов, для перевода необходимой суммы Вам нужно еще: ' + e.Data.toString(), 'Недостаточно голосов', 'OK');
		}
		
		private function OnVoicesWithdrawed(e:SocialDataEvent):void
		{
			FGame.Provider.removeEventListener(VKEvents.VK_VOICES_WITHDRAWED, OnVoicesWithdrawed);
			FGame.User.Money += 1000 * (int(e.Data) / 100);
			FGame.UpdateProfile();
			FGame.ShowInfo('Вы успешно перевели ' + (int(e.Data) / 100).toString() + ' голосов, теперь на вашем счету: ' + FGame.User.Money, 'Успешный перевод', 'OK');			
		}
		
		public function Clear():void
		{
			FGame.Provider.removeEventListener(VKEvents.VK_VOICES_NEEDED, OnVoicesNeeded);
			FGame.Provider.removeEventListener(VKEvents.VK_VOICES_WITHDRAWED, OnVoicesWithdrawed);			
		}
	}	
}
